package com.lhoverc.controller;

import com.lhoverc.annotation.Log;
import com.lhoverc.pojo.Result;
import com.lhoverc.utils.AliOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-04-22
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtil aliOssUtil;

    // 动态获取项目根目录下的 image 文件夹路径
    private final String STATIC_IMAGE_DIR = Paths.get(System.getProperty("user.dir"), "res", "static").toString() + File.separator;
    private final String FACE_IMAGE_DIR = Paths.get(System.getProperty("user.dir"), "res", "face").toString() + File.separator;

    /**
     * 上传照片至云服务
     *
     * @param image
     * @return
     * @throws Exception
     */
    @Log
    @PostMapping("/upload/aliyun")
    public Result upload(MultipartFile image) throws Exception {
        if (image.isEmpty()) {
            return Result.error("上传失败：文件为空");
        }
        log.info("文件上传，文件名：{}", image.getOriginalFilename());

        //阿里云服务存储文件的请求路径
        String url = aliOssUtil.upload(image);

        log.info("文件上传完成，文件访问的url:{}", url);
        return Result.success(url);
    }


    /**
     * 上传照片至本地
     *
     * @param image
     * @return
     * @throws Exception
     */
    @Log
    @PostMapping("/upload/local")
    public Result uploadLocal(@RequestParam("image") MultipartFile image) throws Exception {
        if (image.isEmpty()) {
            return Result.error("上传失败：文件为空");
        }
        log.info("文件上传，文件名：{}", image.getOriginalFilename());


        String originalFilename = image.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = "http://localhost:8090/res/static/" + fileName;
        // 构建保存路径
        String savePath = STATIC_IMAGE_DIR + fileName;
        log.info("保存路径：{}", savePath);
        image.transferTo(new File(savePath));

        log.info("文件上传完成，文件访问的url:{}", url);
        return Result.success(url);
    }

    @PostMapping("/upload/face")
    public Result uploadFace(@RequestParam("image") MultipartFile image) throws Exception {
        if (image.isEmpty()) {
            return Result.error("上传失败：文件为空");
        }
        log.info("文件上传，文件名：{}", image.getOriginalFilename());


        String originalFilename = image.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = "http://localhost:8090/res/static/" + fileName;
        // 构建保存路径
        String savePath = FACE_IMAGE_DIR + fileName;
        log.info("保存路径：{}", savePath);
        image.transferTo(new File(savePath));

        log.info("文件上传完成，文件访问的url:{}", url);
        return Result.success(url);
    }
}
